*******Part 1: impacts on IIM
import delimited "$dir/Model/Simulated_data/simulated_data_cfact_equal_g.csv", clear
gen uniqid = _n
keep uniqid v12 v9
ren v12 pctile_g
ren v9 util_g
tempfile g
save `g'

import delimited "$dir/Model/Simulated_data/simulated_data_cfact_equal_t.csv", clear
gen uniqid = _n
keep uniqid v12 v9
ren v12 pctile_t
ren v9 util_t
tempfile t
save `t'

import delimited "$dir/Model/Simulated_data/simulated_data_cfact_equal_g_avg.csv", clear
gen uniqid = _n
keep uniqid v12 v9
ren v12 pctile_g_avg
ren v9 util_g_avg
tempfile g_avg
save `g_avg'

import delimited "$dir/Model/Simulated_data/simulated_data_cfact_equal_t_avg.csv", clear
gen uniqid = _n
keep uniqid v12 v9
ren v12 pctile_t_avg
ren v9 util_t_avg
tempfile t_avg
save `t_avg'


import delimited "$dir/Model/Simulated_data/simulated_data_base.csv", clear
gen uniqid = _n
keep uniqid v1 v25 v26 v22
ren v1 statefips
ren v22 util
ren v26 pctile
keep if v25<50
merge 1:1 uniqid using `g', keep(match) nogen
merge 1:1 uniqid using `t', keep(match) nogen 
merge 1:1 uniqid using `g_avg', keep(match) nogen
merge 1:1 uniqid using `t_avg', keep(match) nogen 
collapse (mean) pctile* util*, by(statefips)
su pctile*



su util*


********part 2: population elasticity

//state populations in baseline simulation
import delimited "$dir/Model/Simulated_data/simulated_data_base.csv", clear

//            line = [fips[l] fips[lp] skill_prices[l] prim_2010.skill_prices[lp] m mp t e2p e3p h3p_val ap S_par S pops[l] mig e3_fam e3p_fam]


//rename
ren v1 state_born
ren v2 state_move
drop v3 v4
ren v5 parent_marr
ren v6 kid_marr
ren v7 parent_time_inv
ren v8 kid_earn_p2
ren v9 kid_earn_p3
ren v10 kid_hc_p3
ren v11 ability
ren v12 coll_par
ren v13 coll_kid
ren v14 weight
ren v15 mig
ren v16 type
ren v17 mig2
ren v18 lp_norec
ren v19 state_p2
ren v20 race
ren v21 region
ren v22 util
ren v23 parent_fam_earn
ren v24 kid_fam_earn
ren v25 parent_pctile
ren v26 kid_pctile
replace weight = weight*1000000
gen popweight = weight/5000

//in-migration rate
gen count = 1 //total population
gen rawcount = 1/popweight
replace popweight = round(popweight)
collapse (sum) count rawcount [fw=popweight], by(state_move)
//keep count rawcount
save "$temp/pops_baseline", replace

forval i = 1/50{
		import delimited "$dir/Model/Simulated_data/simulated_data_cfact_shock_school_`i'.csv", clear
	ren v1 state_born
	ren v2 state_move
	ren v3 kid_marr
	ren v4 kid_earn_p2
	ren v5 l2
	ren v6 kid_earn_p3
	ren v7 coll_kid
	ren v8 weight
	ren v9 util
	ren v10 parent_fam_earn
	ren v11 kid_fam_earn
	ren v12 parent_pctile
	ren v13 kid_pctile
		egen state_num = group(state_move)
		keep if state_num == `i'
		replace weight = weight*1000000
		gen popweight = weight/5000

		//in-migration rate
		gen count = 1 //total population
		gen rawcount = 1/popweight
		replace popweight = round(popweight)
		collapse (sum) count rawcount [fw=popweight], by(state_move)
		ren count count_cfact
		ren rawcount rawcount_cfact
		save "$temp/pops_cfact_`i'", replace
}

clear
forval i = 1/50{
	append using "$temp/pops_cfact_`i'"
}
merge 1:1 state_move using "$temp/pops_baseline", keep(match) nogen
gen popchange = (count_cfact - count)/count_cfact * 100
su popchange //done


*****part 3: iim impacts
****read in base simulated data
import delimited "$dir/Model/Simulated_data/simulated_data_base.csv", clear



//rename
ren v1 state_born
ren v2 state_move
drop v3 v4
ren v5 parent_marr
ren v6 kid_marr
ren v7 parent_time_inv
ren v8 kid_earn_p2
ren v9 kid_earn_p3
ren v10 kid_hc_p3
ren v11 ability
ren v12 coll_par
ren v13 coll_kid
ren v14 weight
ren v15 mig
ren v16 type
ren v17 mig2
ren v18 lp_norec
ren v19 state_p2
ren v20 race
ren v21 region
ren v22 util
ren v23 parent_fam_earn
ren v24 kid_fam_earn
ren v25 parent_pctile
ren v26 kid_pctile
gen iim = kid_pctile if parent_pctile<50

levelsof state_born, clean local(fips)


collapse (mean) kid_fam_earn iim util, by(state_born)
save "$temp/earnings_base", replace

local counter 0
foreach fip in `fips'{
    local counter `++counter'
	import delimited "$dir/Model/Simulated_data/simulated_data_cfact_shock_school_`counter'.csv", clear
	ren v1 state_born
	ren v2 state_move
	ren v3 kid_marr
	ren v4 kid_earn_p2
	ren v5 l2
	ren v6 kid_earn_p3
	ren v7 coll_kid
	ren v8 weight
	ren v9 util_cfact
	ren v10 parent_fam_earn
	ren v11 kid_fam_earn
	ren v12 parent_pctile
	ren v13 kid_pctile
	keep if state_born == `fip'
	
	
	gen iim_cfact = kid_pctile if parent_pctile<50

	
	collapse (mean) kid_fam_earn iim util
	gen state_born = `fip'
	save "$temp/earnings_cfact_school_shock_`fip'", replace	
}


clear
foreach fip in `fips'{
    append using "$temp/earnings_cfact_school_shock_`fip'"
}
ren kid_fam_earn kid_fam_earn_cfact
save "$temp/earnings_cfact_school_shocks", replace

merge 1:1 state_born using "$temp/earnings_base", keep(match) nogen
gen diff = kid_fam_earn_cfact - kid_fam_earn
su diff
gen diff_iim = iim_cfact - iim
su diff_iim

gen diff_util = util_cfact - util
su diff_util

ren state_born statefips
merge 1:1 statefips using "$data/Crosswalks/state_fips_crosswalk", keep(match) nogen
maptile diff, geo(state) rev n(7) spopt(mos(thick)) legdecimals(2) ///
savegraph("$output/fig6_cfact_shocks_school.png") replace

maptile diff_iim, geo(state) rev n(7) spopt(mos(thick)) legdecimals(2) ///
savegraph("$output/fig6_cfact_shocks_school_iim.png") replace

maptile diff_util, geo(state) rev n(7) spopt(mos(thick)) legdecimals(2) ///
savegraph("$output/fig6_cfact_shocks_school_util.png") replace
graph close

ren diff_iim diff_iim_school
ren statefips stfips
merge 1:1 stfips using "$temp/cfact_results_shocks", keep(match) nogen
corr diff_iim*
